<template>
    <div class="page-account">
        <div v-if="showI18n" class="page-account-header">
            <i-header-i18n />
        </div>
        <div class="page-account-box">

          <div class="page-account-container">
                <div class="page-account-top-desc" title="交通智能护栏在线监控平台">
                  <img src="../../../assets/images/login_tit.png" />
                </div>
              <div class="login-box">
                <div class="page-account-top">
                    用户登录
                </div>
                <Login @on-submit="handleSubmit">
                    <UserName name="username" value="" />
                    <Password name="password" value="" enter-to-submit />

                    <div class="page-account-auto-login">
<!--                        <Checkbox v-model="autoLogin" size="large">{{ $t('page.login.remember') }}</Checkbox>-->
                        <!-- <a href="">{{ $t('page.login.forgot') }}</a> -->
                    </div>
                    <Submit class="sumit-box">{{ $t('page.login.submit') }}</Submit>
                </Login>
              </div>
          </div>

            <!-- <div class="page-account-other">
                <span>{{ $t('page.login.other') }}</span>
                <img src="@/assets/svg/icon-social-wechat.svg" alt="wechat">
                <img src="@/assets/svg/icon-social-qq.svg" alt="qq">
                <img src="@/assets/svg/icon-social-weibo.svg" alt="weibo">
                <router-link class="page-account-register" :to="{ name: 'register' }">{{ $t('page.login.signup') }}</router-link>
            </div> -->
        </div>
        <i-copyright />
    </div>
</template>
<script>
    import iCopyright from '@/components/copyright';
    import { mapActions } from 'vuex';
    import mixins from '../mixins';
    import {AccountLogin} from "@/api/account"
    import tool from '@/libs/tool';
    import cookies from "@/libs/util.cookies";
    import Setting from '@/setting';

    export default {
        mixins: [ mixins ],
        components: { iCopyright },
        created() {
          this.$store.commit('admin/websoket/changeData',[{name:'isWebSoket',value:false},{name:'websoketData',value:{}}]);
          this.$store.commit('admin/screen/changeData',[{name:'region_id',value:''}]);
        },
      data () {
            return {
                autoLogin: true
            }
        },
        methods: {
            ...mapActions('admin/account', [
                'login'
            ]),
            /**
             * @description 登录
             * 表单校验已有 iView Pro 自动完成，如有需要修改，请阅读 iView Pro 文档
             */
            handleSubmit (valid, values) {
                if (valid) {
                  window.localStorage.setItem('access_token','');
                  const { username, password } = values;
                  AccountLogin({
                        phone:username,
                        password
                    })
                        .then(async (data) => {
                           if(data.code==0){
                             // let jwt =tool.getjwt(_.get(data,'data.id'));
                             // window.localStorage.setItem('access_token',jwt);
                             window.localStorage.setItem('userInfo',JSON.stringify(data.data));
                             cookies.set('uuid',_.get(data,'data.id'));
                             cookies.set('token',_.get(data,'data.token'));
                             // 结束
                             if(_.get(data,'data.isRoot')==1){
                               data.data.access=['root']
                             }

                             await this.$store.dispatch('admin/user/set', data.data, { root: true });
                             // 用户登录后从持久化数据加载一系列的设置
                             await this.$store.dispatch('load', { loadOpenedTabs: Setting.page.loadOpenedTabs });

                             // 重定向对象不存在则返回顶层路径
                             this.$router.replace(this.$route.query.redirect || '/');
                           }else{
                             this.$Message.error(_.get(data,'登录失败')||'登录失败！')
                           }

                    });
                }
            },
            /**
             * @description 获取验证码
             * */
            handleGetCaptcha () {

            }
        },
        beforeDestroy() {
          this.$store.commit('admin/websoket/changeData',{name:'isWebSoket',value:true})
        }
    };
</script>
<style lang="less" scoped>
.page-account-top-desc{
  font-size: 20px;
  img{
    max-width:100%;
  }
}
</style>
